package com.clone.surpassli.clone_cloudreader.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.clone.surpassli.clone_cloudreader.MainActivity;
import com.clone.surpassli.clone_cloudreader.R;
import com.clone.surpassli.clone_cloudreader.bean.book.BooksBean;
import com.clone.surpassli.clone_cloudreader.databinding.FootItemBookBinding;
import com.clone.surpassli.clone_cloudreader.databinding.HeaderItemBookBinding;
import com.clone.surpassli.clone_cloudreader.databinding.ItemBookBinding;
import com.clone.surpassli.clone_cloudreader.utils.PerfectClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SurpassLi on 2017/10/12.
 * BookAdapter
 */

public class BookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private MainActivity context;

    private int status = 1;
    public static final int LOAD_MORE = 0;
    public static final int LOAD_PULL_TO = 1;
    public static final int LOAD_NONE = 2;
    private static final int LOAD_END = 3;

    private static final int TYPE_FOOTER_BOOK = -2;  // 界面最后的部分，显示加载更多
    private static final int TYPE_HEADER_BOOK = -3;  // 界面头部
    private static final int TYPE_CONTENT_BOOK = -4; // 界面显示部分
    private List<BooksBean> list;

    public BookAdapter(Context context) {
        this.context = (MainActivity) context;
        list = new ArrayList<>();
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER_BOOK;
        } else if (position + 1 == getItemCount()) {
            return TYPE_FOOTER_BOOK;
        } else {
            return TYPE_CONTENT_BOOK;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEADER_BOOK:
                HeaderItemBookBinding mBindingHeader = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.header_item_book, parent, false);
                return new HeaderViewHolder(mBindingHeader.getRoot());
            case TYPE_FOOTER_BOOK:
                FootItemBookBinding mBindingFoot = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.foot_item_book, parent, false);
                return new FooterViewHolder(mBindingFoot.getRoot());
            default:
                ItemBookBinding itemBookBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_book, parent, false);
                return new BookViewHolder(itemBookBinding.getRoot());
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
            headerViewHolder.bindItem();
        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            footerViewHolder.bindItem();
        } else if (holder instanceof BookViewHolder) {
            BookViewHolder bookViewHolder = (BookViewHolder) holder;
            if (list != null && list.size() > 0) {
                bookViewHolder.bindItem(list.get(position - 1), position - 1);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size() + 2;
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {

        HeaderItemBookBinding mBindBook;

        HeaderViewHolder(View view) {
            super(view);
            mBindBook = DataBindingUtil.getBinding(view);
        }

        private void bindItem() {
//            mBindBook.setBean(book);
//            mBindBook.executePendingBindings();
        }
    }

    private class FooterViewHolder extends RecyclerView.ViewHolder {

        FootItemBookBinding mBindFooter;

        FooterViewHolder(View view) {
            super(view);
            mBindFooter = DataBindingUtil.getBinding(view);
            mBindFooter.rlMore.setGravity(Gravity.CENTER);
        }

        private void bindItem() {
            switch (status) {
                case LOAD_MORE:
                    mBindFooter.progress.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setText("正在加载...");
                    itemView.setVisibility(View.VISIBLE);
                    break;
                case LOAD_PULL_TO:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("上拉加载更多");
                    itemView.setVisibility(View.VISIBLE);
                    break;
                case LOAD_NONE:
                    System.out.println("LOAD_NONE----");
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("没有更多内容了");
                    break;
                case LOAD_END:
                    itemView.setVisibility(View.GONE);
                default:
                    break;
            }
        }
    }

    private class BookViewHolder extends RecyclerView.ViewHolder {

        ItemBookBinding mBindBook;

        BookViewHolder(View view) {
            super(view);
            mBindBook = DataBindingUtil.getBinding(view);
        }

        private void bindItem(final BooksBean book, int position) {
            mBindBook.setBean(book);
            mBindBook.executePendingBindings();

            mBindBook.llItemTop.setOnClickListener(new PerfectClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
//                    BookDetailActivity.start(context,book,mBindBook.ivTopPhoto);
                }
            });
        }
    }

    public List<BooksBean> getList() {
        return list;
    }

    public void setList(List<BooksBean> list) {
        this.list.clear();
        this.list = list;
    }

    public void addAll(List<BooksBean> list) {
        this.list.addAll(list);
    }

    public void updateLoadStatus(int status) {
        this.status = status;
        notifyDataSetChanged();
    }

    public int getLoadStatus(){
        return this.status;
    }
}
